package com.hzqy.service.plan;

import com.hzqy.persistence.dao.hqjc.HqjcDao;
import com.hzqy.persistence.dao.plan.UserCompleteCourseDao;
import com.hzqy.persistence.dao.plan.UserPlanDao;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.CmSeriesVo;
import com.hzqy.web.vo.UserCompleteCourseVo;
import com.hzqy.web.vo.UserPlanVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class PlanServiceBean {
    protected final Logger LOG = LoggerFactory.getLogger(getClass());

    @Resource
    private HqjcDao hqjcDao;

    @Resource
    private UserPlanDao userPlanDao;

    @Resource
    private UserCompleteCourseDao userCompleteCourseDao;

    public int insertUserPlanVo(UserPlanVo userPlanVo){
        return userPlanDao.insertUserPlan(userPlanVo);
    }

    public int insertUserCompleteCourseVo(UserCompleteCourseVo userCompleteCourseVo){
        int flag = userCompleteCourseDao.insertUserCompleteCourse(userCompleteCourseVo);
        if(flag>0){//插入成功后，查询当前计划时候全部完成，全部完成，更新计划为已完成
            Map<String,Object> map = new HashMap<>();
            map.put("f_up_id",userCompleteCourseVo.getF_up_id());
            map.put("f_up_plan_id",userCompleteCourseVo.getF_ucc_plan_id());
            map.put("f_up_serverid",userCompleteCourseVo.getF_ucc_serverid());
            CmContentVo cmContentVo = userPlanDao.selectUserPlanById(map);
            int courseCount = cmContentVo.getF_cc_obligate3();//课程数
            String starttime = cmContentVo.getF_up_starttime();//计划开始时间
            LOG.debug("starttime："+starttime);
            List<CmSeriesVo> cmSeriesVos = hqjcDao.selectAllSeries(cmContentVo);
            int completeResult = 0;
            for(CmSeriesVo cmSeriesVo:cmSeriesVos){
                String f_cc_ids[] = cmSeriesVo.getF_ssc_code().split(",");
                for(String f_cc_id : f_cc_ids){
                    CmContentVo ccv = new CmContentVo();
                    ccv.setF_cc_id(Integer.parseInt(f_cc_id));
                    ccv.setF_cc_cprid("3");
                    CmContentVo c = hqjcDao.selectContent(ccv);
                    if(!"rest".equals(c.getF_cc_duration())){
                        UserCompleteCourseVo userCompleteCourse = new UserCompleteCourseVo();
                        userCompleteCourse.setF_ucc_serverid(userCompleteCourseVo.getF_ucc_serverid());
                        userCompleteCourse.setF_ucc_contentid(Integer.parseInt(f_cc_id));
                        userCompleteCourse.setF_ucc_plan_id(userCompleteCourseVo.getF_ucc_plan_id());
                        userCompleteCourse.setF_ucc_indexview(cmSeriesVo.getF_ssc_viewindex());
                        userCompleteCourse.setF_ucc_seriesid(cmSeriesVo.getF_ssc_id());
                        userCompleteCourse.setF_up_id(userCompleteCourseVo.getF_up_id());
                        userCompleteCourse.setF_up_starttime(starttime);
                        LOG.debug("userCompleteCourseVo.starttime："+userCompleteCourse.getF_up_starttime());
                        int Result = calculationPlanResult(userCompleteCourse);
                        LOG.debug("Result："+Result+";f_cc_id="+f_cc_id);
                        if(Result==1){
                            completeResult++;
                        }
                    }else{
                        completeResult++;
                    }
                }
            }
            LOG.debug("完成数："+completeResult+"---课程数："+courseCount+"---剧集数："+cmSeriesVos.size());
            //课程数与完成数相等，或者剧集数为0
            if(completeResult == courseCount || cmSeriesVos.size() == 0){
                UserPlanVo userPlanVo = new UserPlanVo();
                userPlanVo.setF_up_id(userCompleteCourseVo.getF_up_id());
                userPlanVo.setF_up_status(4);
                updateUserPlan(userPlanVo);
            }
        }
        return flag;
    }

    public int updateUserPlan(UserPlanVo userPlanVo){
        int flag = userPlanDao.updateStatusById(userPlanVo);
        if(flag > 0 && userPlanVo.getF_up_status() == 1){//重启计划
            Map<String,Object> map = new HashMap<>();
            map.put("f_up_id",userPlanVo.getF_up_id());
            UserPlanVo userPlanVo1 = userPlanDao.selectUserPlan(map);
            map.put("f_ucc_plan_id",userPlanVo1.getF_up_plan_id());
            map.put("f_ucc_serverid",userPlanVo1.getF_up_serverid());
            userCompleteCourseDao.deleteUserCompleteCourse(map);
        }
        return flag;
    }

    public List<CmContentVo> selectPlanList(String serverid){
        Map<String,Object> map = new HashMap<>();
        map.put("f_up_serverid",serverid);
        List<CmContentVo> result = userPlanDao.selectUserPlanList(map);
        return result;
    }

    public Map<String,Object> selectPlanDetial(CmContentVo contentVo,String f_up_serverid){
        Map<String,Object> map = new HashMap<>();
        Map<Object,Object> m = new HashMap<>();
        map.put("f_up_id",contentVo.getF_up_id());
        map.put("f_up_plan_id",contentVo.getF_cc_id());
        map.put("f_up_serverid",f_up_serverid);
        CmContentVo planInfo = userPlanDao.selectUserPlanById(map);
        String starttime = planInfo.getF_up_starttime();//计划开始时间
        map.put("planInfo",planInfo);//计划详情
        List<CmSeriesVo> cmSeriesVos = hqjcDao.selectAllSeries(contentVo);
        List<List<CmContentVo>> result = new ArrayList<>();
        for(CmSeriesVo cmSeriesVo:cmSeriesVos){
            String f_cc_ids[] = cmSeriesVo.getF_ssc_code().split(",");
            List<CmContentVo> list = new ArrayList<>();
            for(String f_cc_id : f_cc_ids){
                CmContentVo ccv = new CmContentVo();
                ccv.setF_cc_id(Integer.parseInt(f_cc_id));
                ccv.setF_cc_cprid(contentVo.getF_cc_cprid());
                CmContentVo c = hqjcDao.selectContent(ccv);
                UserCompleteCourseVo userCompleteCourseVo = new UserCompleteCourseVo();
                userCompleteCourseVo.setF_ucc_serverid(f_up_serverid);
                userCompleteCourseVo.setF_ucc_contentid(Integer.parseInt(f_cc_id));
                userCompleteCourseVo.setF_ucc_plan_id(contentVo.getF_cc_id());
                userCompleteCourseVo.setF_ucc_indexview(cmSeriesVo.getF_ssc_viewindex());
                userCompleteCourseVo.setF_ucc_seriesid(cmSeriesVo.getF_ssc_id());
                userCompleteCourseVo.setF_up_id(contentVo.getF_up_id());
                userCompleteCourseVo.setF_up_starttime(starttime);
                int Result = calculationPlanResult(userCompleteCourseVo);
                CmContentVo tempc = (CmContentVo)c.clone();//浅拷贝，避免相同的内容c，后面修改的时候改变前面的值
                tempc.setF_up_result(Result);
                list.add(tempc);
            }

            result.add(list);
        }
        map.put("seriesList",result);
        return map;
    }

    public CmContentVo selectCmContentVoByPlanCondition(CmContentVo contentVo){
        CmContentVo ccv = userPlanDao.selectCmContentVoByPlanCondition(contentVo);
        if(ccv == null){
            //未匹配到计划，用最新的计划，默认计划
            ccv = userPlanDao.selectCmContentVoByPlanCondition(contentVo);
        }
        return ccv;
    }

    /**
     * 用户某天某内容是否完成
     *
     * @param userCompleteCourseVo
     * @return
     */
    public int calculationPlanResult(UserCompleteCourseVo userCompleteCourseVo){
        Map<String,Object> map =  new HashMap<>();
        CmContentVo cmContentVo = new CmContentVo();
        cmContentVo.setF_cc_id(userCompleteCourseVo.getF_ucc_contentid());
        List<CmSeriesVo> cmSeriesVos = hqjcDao.selectAllSeries(cmContentVo);
        map.put("f_ucc_serverid",userCompleteCourseVo.getF_ucc_serverid());
        map.put("f_ucc_contentid",userCompleteCourseVo.getF_ucc_contentid());
        map.put("f_ucc_plan_id",userCompleteCourseVo.getF_ucc_plan_id());
        map.put("f_ucc_indexview",userCompleteCourseVo.getF_ucc_indexview());
        map.put("f_up_id",userCompleteCourseVo.getF_up_id());
        map.put("f_up_starttime",userCompleteCourseVo.getF_up_starttime());
        UserCompleteCourseVo ucc = null;
        int result = 1;//已完成
        if(cmSeriesVos.size()>0){
            for(CmSeriesVo cmSeriesVo : cmSeriesVos){
                map.put("f_ucc_seriesid",cmSeriesVo.getF_ssc_id());
                ucc = userCompleteCourseDao.selectUserCompleteCourseInfo(map);
                if(ucc == null || ucc.getF_ucc_status() != 1){//未完成
                    result = 0;
                }
            }
        }else{
            ucc = userCompleteCourseDao.selectUserCompleteCourseInfo(map);
            if(ucc == null || ucc.getF_ucc_status() != 1){//未完成
                result = 0;
            }
        }
        return result;
    }

    public Map<String,Object> calculationPlanResults(UserCompleteCourseVo userCompleteCourseVo){
        Map<String,Object> map =  new HashMap<>();
        int result =  calculationPlanResult(userCompleteCourseVo);
        map.put("result",result);
        return map;
    }
}
